<?php
/**
 * Ipx link is only usable on wap pages.
 * 
 * Ipx link uses userData. Do not use in userdata Array these keys: IpxSessionId, phoneNr, operator.
 * Operator name and phoneNr will be added to userData when payment is finished.
 *
 */
class IpxLink extends BankLink {
	
	protected $username;
	
	protected $password;
	
	private $wsdl = 'http://europe.ipx.com/api/services2/PurchaseApi30?wsdl';
	
	public function sendToBank( $callBackId, BankPayment $payment ) {

		$soapClient = new SoapClient($this->wsdl);
		
		$request = array(
			'correlationId' => $callBackId, 
			'tariffClass' => strtoupper($payment->currency).''.($payment->price*100), 
			'returnURL'=> $this->callbackUrl . $callBackId, 
			'contentName' => $payment->description, 
			'contentCategory' => '#NULL#', 
			'contentMetaData' => '#NULL#',
			'language' => '#NULL#', 
			'VAT' => -1,
			'username' => $this->username, 
			'password' => $this->password
		);
			
		$result = $soapClient->createSession( $request );
		
		// update log entry with IPX sessionId
		$callBack = BankLog::load( $callBackId );
		$data = $callBack->userData;
		$data['IpxSessionId'] = $result->sessionId;
		$callBack->userData = $data;
		$callBack->save();
		// ---------------

		if( $result->responseCode === 0 ) 
			header('location:'.$result->redirectURL);
		else 
			throw new RuntimeException('Sending to IPX bank failed!');			
	}
	
	public function handleCallback() {
		
		$callBackId = $_REQUEST['BankLinkCallBackId'];
		$logEntry = BankLog::load( $callBackId );
		$sessionId = $logEntry->userData['IpxSessionId'];

		$request =  array(
			'correlationId' => $callBackId, 
			'sessionId' => $sessionId, 
			'username' => $this->username, 
			'password' => $this->password
		);

		// Check if session is ok and payment authorised
		$soapClient = new SoapClient($this->wsdl);
		$result = $soapClient->checkStatus( $request );
		

		if( !($result->statusMessage === 'Authorized' && $result->responseCode === 0 && $result->correlationId === $callBackId) )
			return array('status' => 3, 'logId' => $callBackId, 'bankError'=>'', 'bankErrorStr'=>'sessionId:'.$sessionId.'|responseMessage'.$result->responseMessage);
		// ----------------------------------------------
		
				
		// Get customer phoneNr
		$soapClient = new SoapClient($this->wsdl);
		$result = $soapClient->getConsumerId( $request );
		$data = $logEntry->userData;
		unset($data['IpxSessionId']);
		$data['phoneNr'] = $result->consumerId;
		$data['operator'] = $result->operator;
		$logEntry->userData = $data;
		$logEntry->save();
		// ----------------------------------------------
		
		
		// Finish payment
		$soapClient = new SoapClient($this->wsdl);
		$result = $soapClient->finalizeSession( $request );
			
		if( $result->responseCode === 0)
			return array('status' => 1, 'logId' => $callBackId, 'bankError'=>'', 'bankErrorStr'=>'sessionId:'.$sessionId.'|transactionId:'.$result->transactionId.'|responseMessage'.$result->responseMessage );
		else
			return array('status' => 3, 'logId' => $callBackId, 'bankError'=>'', 'bankErrorStr'=>'sessionId:'.$sessionId.'|transactionId:'.$result->transactionId.'|responseMessage'.$result->responseMessage);
		// ----------------------------------------------
	}

}
?>